package com.xwh.article.client;

import com.xwh.core.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * 用户
 *
 * @author xwh
 **/
@Service
public class SystemService {

    private final WebClient webClient;

    @Autowired
    public SystemService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("lb://cms-system").build();
    }

    public Mono<Result> findById(String id) {
        return webClient.get()
                .uri("/user/{id}", id)
                .retrieve()
                .bodyToMono(Result.class)
                .doOnError(Throwable::printStackTrace);
    }

    public void saveResourceIsUpdate(String apiList, String service) {
        webClient.post()
                .uri("/resource/addAll/{service}", service)
                .bodyValue(apiList)
                .retrieve()
                .bodyToMono(Result.class)
                .doOnError(Throwable::printStackTrace);
    }

    public Mono<Result> get(String name) {
        return webClient.get()
                .uri("/dict/{name}", name)
                .retrieve()
                .bodyToMono(Result.class)
                .doOnError(Throwable::printStackTrace);
    }

    public Mono<Result> getLabel(String name, String value) {
        return webClient.get()
                .uri("/dict/getLabel/{name}/{value}", name, value)
                .retrieve()
                .bodyToMono(Result.class)
                .doOnError(Throwable::printStackTrace);
    }
}